package com.stationbuilding.adapter.document;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.stationbuilding.MyApplication;
import com.stationbuilding.R;
import com.stationbuilding.bean.DocumentBean;
import com.stationbuilding.bean.MemberBean;

import java.util.List;

public class DocumentListAdapter extends BaseAdapter {

    private Context mContext;
    private List<DocumentBean> list;

    private int selectedPosition = -1;

    public DocumentListAdapter(@NonNull List<DocumentBean> list) {
        this.mContext = MyApplication.getContext();
        this.list = list;
    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<DocumentBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setList(List<DocumentBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        DocumentBean documentBean = list.get(position);
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.document_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.documentImg = (ImageView) convertView.findViewById(R.id.document_img);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (documentBean.getDocumentType() == 0) {
            Glide.with(mContext).load(R.mipmap.folder).into(viewHolder.documentImg);
        } else {
            Glide.with(mContext).load(R.mipmap.document).into(viewHolder.documentImg);
        }
        viewHolder.name.setText(documentBean.getDocumentName());

        if (selectedPosition == position) {
            convertView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.dodgerblue));
        } else {
            convertView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
        }

        return convertView;
    }

    private static class ViewHolder {
        private ImageView documentImg;
        private TextView name;
    }
}
